/**
 * @author mamat-dev
 */
package com.googlecode.explorecode.mamat.sendimage;

import javax.microedition.media.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.control.*;
import java.io.IOException;

class CameraForm extends Form implements CommandListener {

    private final CameraMIDlet midlet;
    private final Command exitCommand;
    private Command captureCommand = null;
    private Command showImageCommand = null;
    private Player player = null;
    private VideoControl videoControl = null;
    private boolean active = false;
    private StringItem messageItem;

    public CameraForm(CameraMIDlet midlet) {
        super("Camera");
        this.midlet = midlet;
        messageItem = new StringItem("VIDEO", "start");
        append(messageItem);
        exitCommand = new Command("EXIT", Command.EXIT, 1);
        addCommand(exitCommand);
        setCommandListener(this);
        try {
            //buat player baru dan set untuk ditampilkan
            player = Manager.createPlayer("capture://video");
            player.realize();
            //Grap Video control dan tampilkan di layar sekarang.
            videoControl = (VideoControl) (player.getControl("VideoControl"));
            if (videoControl != null) {
                append((Item) (videoControl.initDisplayMode(
                        VideoControl.USE_GUI_PRIMITIVE, null)));
                captureCommand = new Command("CAPTURE", Command.SCREEN, 1);
                addCommand(captureCommand);
                messageItem.setText("OK");
            } else {
                messageItem.setText("Tidak ada video control");
            }
        } catch (IOException ioe) {
            messageItem.setText("IOException: " + ioe.getMessage());
        } catch (MediaException me) {
            messageItem.setText("Media Exception: " + me.getMessage());
        } catch (SecurityException se) {
            messageItem.setText("Security Exception: " + se.getMessage());
        }
    }

    /**
     *	Video harus ditampikan di screen sebelum memulai kita harus jalankan
     * playernya dan set videoControl jadi visible.
     **/
    synchronized void start() {
        if (!active) {
            try {
                if (player != null) {
                    player.start();
                }
                if (videoControl != null) {
                    videoControl.setVisible(true);
                }
            } catch (MediaException me) {
                messageItem.setText("Media Exception: " + me.getMessage());
            } catch (SecurityException se) {
                messageItem.setText("Security Exception: " + se.getMessage());
            }
            active = true;
        }
    }

    /**
     * untuk men-stop player. Pertama kali videoControl harus di set invisible
     * kemudia player bisa di-stop.
     **/
    synchronized void stop() {
        if (active) {
            try {
                if (videoControl != null) {
                    videoControl.setVisible(false);
                }
                if (player != null) {
                    player.stop();
                }
            } catch (MediaException me) {
                messageItem.setText("Wew error meneh Media Exception: " + me.getMessage());
            }
            active = false;
        }
    }

    /**
     *	eventHandling
     *  di captureCommand gambar diambil dan di kirim ke server
     *
     **/
    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            midlet.cameraFormExit();
        } else {
            if (c == captureCommand) {
                
                try {
                    byte[] image = videoControl.getSnapshot("encoding=jpeg&width=160&height=120");
                    midlet.transmitImage(image);
                    messageItem.setText("Capture OK");
                } catch (Exception me) {
                    messageItem.setText("Media Exception: " + me.getMessage());
                }

            }
        }
    }
}